package ru.arriah.servicedesk.web.admin.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.DepartmentBean;
import ru.arriah.servicedesk.bean.error.DepartmentFormErrorBean;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalHome;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class ProcessDepartmentFormAction extends DumbAction {
   protected DepartmentFormErrorBean departmentFormErrorBean;
   protected OrganizationManagerLocalHome organizationManagerHome;

   public ProcessDepartmentFormAction() throws NamingException {
      super();
      
      organizationManagerHome =
        Utils.getOrganizationManagerHomeInterface();
   }
   
   
   private boolean isValid(
     DepartmentBean departmentBean    
   ) throws InternalException {
     boolean valid = true;
     try {
       departmentFormErrorBean = new DepartmentFormErrorBean();
             
     if (departmentBean.getName().equals("") || departmentBean.getName() == null) {
       departmentFormErrorBean.setName(
         config.getProperty("ERROR.DEPARTMENTFORM.NAME")
       );
       valid = false;
     }
                   
     return valid;
     } catch (Exception e) {
       handleException(e, "isValid");
       throw new InternalException("ErrorMessage.internal");
     }
   }

   
   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
     try {
       DepartmentBean departmentBean = 
          new DepartmentBean(); 
              
       departmentBean.setName(request.getParameter("name"));
       departmentBean.setId(idVal(request.getParameter("department_id")));
       departmentBean.setOrganizationId(idVal(request.getParameter("organization_id")));
       
       if(!isValid(departmentBean)) {
         addErrorMessage(
            request,
            config.getProperty("ERROR.DEPARTMENTFORM_FIELDS_INVALID")
         );
         
         request.setAttribute("error", departmentFormErrorBean);
                
         return actionMapping.getFailureTarget();
       }       
       
       OrganizationManagerLocalObject organizationManager = 
         organizationManagerHome.create();
       
       if(departmentBean.getId() == 0) {
         organizationManager.addDepartment(departmentBean);
         
         addInfMessage(
            request,
            config.getProperty("INF.DEPARTMENT.ADDED")
         );
         
       } else {         
         organizationManager.updateDepartment(departmentBean);
         
         addInfMessage(
            request,
            config.getProperty("INF.DEPARTMENT.UPDATED")           
         );
       }
       
       //DISPLAY DEPARTMENTS LIST
                    
       int organizationId = 
         idVal(request.getParameter("organization_id"));
       
       Collection<DepartmentBean> departmentsList = 
         organizationManager.selectDepartments(
           organizationId,
           "name", 
           "ASC",
           "'active'"
         );
       
       organizationManager.remove();              
       request.setAttribute("organizationId", organizationId);              
       request.setAttribute("departmentsList", departmentsList);                         
       
       return actionMapping.getSuccessTarget();
    } catch (Exception e) {
       handleException(e, "execute");
       throw new InternalException("ErrorMessage.internal");
    }
   }
}